//
//  OfficalMail.m
//  OASystem
//
//  Created by macos on 14-4-17.
//  Copyright (c) 2014年 macos. All rights reserved.
//

#import "OfficalMail.h"

@interface OfficalMail ()

@end

@implementation OfficalMail
@synthesize noReadStr;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"LOAD" object:nil];
    //获取收件箱未读邮件
    [[SendHttpOperation shareSendHttpOperation] GetNoReadCount:@"GetNoReadCount.ashx"
                                                   BoxTypeGuid:@"100000"
                                                 ownerUserGuid:USER_OWNER_GUDID];
    [SendHttpOperation shareSendHttpOperation].getNoReadCountDelegate = self;
}

//获取公告列表回调
- (void)passNoReadCount:(NSDictionary *)NoReadCountDic
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"REMOVE" object:nil];
    noReadStr = [NSString stringWithFormat:@"%@",[[NoReadCountDic objectForKey:@"result"] objectForKey:@"value"]];
    [officalMainTable reloadData];
}

//返回上一级
- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)initNavi
{
    //初始化UINavigation
    self.navigationItem.titleView = [[UIView alloc] initWithFrame:CGRectMake(110, 0, 100, 44)];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    titleLabel.text = @"公务邮箱";
    titleLabel.font = HEIGHT_FONT(18.0f);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor whiteColor];
    [self.navigationItem.titleView addSubview:titleLabel];
    
    //添加导航又边用户信息
    //初始化UINavigation左按钮
    UIButton *helloButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 10, 80, 24)];
    [helloButton setTitle:[NSString stringWithFormat:@"你好，%@",USER_NICK_NAME] forState:UIControlStateNormal];
    helloButton.titleLabel.font = HEIGHT_FONT(13.0f);
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:helloButton];
    
    //初始化UINavigation左按钮
    UIButton *naviButton = [[UIButton alloc] initWithFrame:CGRectMake(5, 10, 45, 24)];
    [naviButton setTitle:@"首页" forState:UIControlStateNormal];
    naviButton.titleLabel.font = HEIGHT_FONT(13.0f);
    [naviButton setBackgroundImage:[UIImage imageNamed:@"title_back_bg_n.png"] forState:UIControlStateNormal];
    [naviButton setBackgroundImage:[UIImage imageNamed:@"title_back_bg_p.png"] forState:UIControlStateHighlighted];
    [naviButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:naviButton];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"mail_bg.png"]];
    
    [self initNavi];
    
    selectIndexRow = 4;//默认cell都未选中
    officalMainTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    imgArr = @[@"mail.png",@"mail2.png",@"fs.png",@"mail_hvoer.png",@"mail2_hover.png",@"fs_hover.png"];
    
    [writeMailButton setBackgroundImage:[UIImage imageNamed:@"but_hover.png"] forState:UIControlStateHighlighted];
}

//写邮件
- (IBAction)writeMail:(id)sender
{
    WriteEmailView *instance= [[WriteEmailView alloc] init];
    instance.boolDraft = 0;
    [self.navigationController pushViewController:instance animated:YES];
}

#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    
    NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
    for (UILabel *subview in subviews)
    {
        [subview removeFromSuperview];
    }
    
    
    //自定义分割线
    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, 45, 320, 1)];
    line.backgroundColor = NOTICE_CONTENT_BACKCOLOR;
    [cell.contentView addSubview:line];
    
    //icon图标
    UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(20, (indexPath.row==1?12:15), 21, (indexPath.row==1?21:14))];
    icon.image = [UIImage imageNamed:[imgArr objectAtIndex:(indexPath.row == selectIndexRow?indexPath.row+3:indexPath.row)]];
    [cell.contentView addSubview:icon];
    
    //每行标题
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(60, 12, 80, 21)];
    title.text = indexPath.row==0?@"收件箱":(indexPath.row == 1?@"草稿箱":@"已发送");
    title.backgroundColor = [UIColor clearColor];
    title.font = HEIGHT_FONT(14.0f);
    title.textColor = indexPath.row == selectIndexRow?[UIColor whiteColor]:[UIColor blackColor];
    [cell.contentView addSubview:title];
    
    if(indexPath.row == 0)
    {
        NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
        for (UIView *subview in subviews)
        {
            if(subview.tag == 100 ||subview.tag == 101)
                [subview removeFromSuperview];
        }
        
        if([noReadStr intValue] != 0)
        {
            //消息条数背景
            UIImageView *numBackImage = [[UIImageView alloc] initWithFrame:CGRectMake(220, 14, 30, 17)];
            numBackImage.image = [UIImage imageNamed:indexPath.row == selectIndexRow?@"pic1_hover.png":@"pic1.png"];
            numBackImage.tag = 100;
            [cell.contentView addSubview:numBackImage];
            
            UILabel *numCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 17)];
            numCountLabel.text = noReadStr;
            numCountLabel.tag = 101;
            numCountLabel.font = HEIGHT_FONT(12.0f);
            numCountLabel.textAlignment = NSTextAlignmentCenter;
            numCountLabel.textColor = (indexPath.row == selectIndexRow?[UIColor blackColor]:[UIColor whiteColor]);
            numCountLabel.backgroundColor = [UIColor clearColor];
            [numBackImage addSubview:numCountLabel];
        }
    }
    
    //向右图标
    UIImageView *directImg = [[UIImageView alloc] initWithFrame:CGRectMake(280, 16, 11, 13)];
    directImg.image = [UIImage imageNamed:indexPath.row == selectIndexRow?@"pic2_hover.png":@"pic2.png"];
    [cell.contentView addSubview:directImg];
    
    cell.contentView.backgroundColor = (indexPath.row == selectIndexRow?[UIColor colorWithPatternImage:[UIImage imageNamed:@"navbg.png"]]:[UIColor clearColor]);
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    selectIndexRow = indexPath.row;
    [officalMainTable reloadData];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"LOAD" object:nil];
    
    //获取邮件列表
    [[SendHttpOperation shareSendHttpOperation] GetMailList:@"GetMailList.ashx"
                                                        Cur:@"0"
                                                       Rows:@"10"
                                                BoxTypeGuid:indexPath.row ==0?@"100000":((indexPath.row == 1)?@"100002":@"100001")
                                                   haveread:@"2"
                                              ownerUserGuid:USER_OWNER_GUDID];
    [SendHttpOperation shareSendHttpOperation].getOfficalMailDelegate = self;
}

//获取邮件列表后的回调
- (void)passOfficalMail:(NSDictionary *)officalMailDic
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"REMOVE" object:nil];
    
    MailListView *instance = [[MailListView alloc] init];
    instance.mailTypeStr = selectIndexRow ==0?@"100000":((selectIndexRow == 1)?@"100002":@"100001");
    instance.mailListMutArr = [NSMutableArray arrayWithArray:[[[officalMailDic objectForKey:@"result"] objectForKey:@"list"] objectForKey:@"data"]];
    [self.navigationController pushViewController:instance animated:YES];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
